/*
 * Example7Threads.java  0.4.0 / 2010-10-22
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.examples;

import java.util.Random;

import com.cute4j.swing.Button;
import com.cute4j.swing.Container;
import com.cute4j.swing.ProgressBar;
import com.cute4j.swing.Swing;
import com.cute4j.swing.events.ActionEvent;
import com.cute4j.swing.events.ActionListener;

/**
 * Threads Example.
 */
public class Example9Threads extends Container {

  final ProgressBar progressBar1;
  final ProgressBar progressBar2;
  final Button button;
  final RandomThread randomThread11;
  final RandomThread randomThread12;
  final RandomThread randomThread21;
  final RandomThread randomThread22;

  /**
   * GUI Building.
   */
  public Example9Threads() {
    super(null);
    setName("Threads");
    setFixedSize(300, 100);
    progressBar1 = new ProgressBar();
    progressBar1.setValue(50);
    add(progressBar1);
    progressBar1.setBounds(10, 10, 274, 20);
    progressBar2 = new ProgressBar();
    progressBar2.setValue(50);
    add(progressBar2);
    progressBar2.setBounds(10, 40, 274, 20);
    button = new Button("Pause");
    add(button);
    button.setBounds(120, 70, 60, 25);
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (randomThread11.isWorking()) {
          randomThread11.pauseTimer();
          randomThread12.pauseTimer();
          randomThread21.pauseTimer();
          randomThread22.pauseTimer();
          button.setText("Resume");
        } else {
          randomThread11.resumeTimer();
          randomThread12.resumeTimer();
          randomThread21.resumeTimer();
          randomThread22.resumeTimer();
          button.setText("Pause");
        }
      }
    });
    randomThread11 = new RandomThread(progressBar1);
    randomThread12 = new RandomThread(progressBar1);
    randomThread21 = new RandomThread(progressBar2);
    randomThread22 = new RandomThread(progressBar2);
  }

  /**
   * Non-GUI Thread.
   */
  static class RandomThread extends Thread {
    private static final Random random = new Random();
    private boolean pause;
    private final ProgressBar progressBar;
    public RandomThread(ProgressBar progressBar) {
      this.progressBar = progressBar;
      setDaemon(true);
      start();
    }
    public synchronized boolean isWorking() {
      return !pause;
    }
    public synchronized void pauseTimer() {
      pause = true;
      notifyAll();
    }
    public synchronized void resumeTimer() {
      pause = false;
      notifyAll();
    }
    public void run() {
      try {
        while (true) {
          sleep(500 + random.nextInt(1000));
          synchronized (this) {
            while (pause) {
              wait();
            }
          }
          int value = 50 + (int)(40 * random.nextGaussian());
          progressBar.setValue(value); // <-- regular call from non-GUI thread
        }
      } catch (InterruptedException e) {
      }
    }
  }

  public static void main(String[] args) {
    Example9Threads threadsExample = new Example9Threads();
    threadsExample.setVisible(true);
    Swing.startEventLoop();
  }

}
